package cn.wolfcode.p2p.base.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.wolfcode.p2p.base.domain.OrderTime;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.mapper.OrderTimeMapper;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.query.VideoAuthQueryObject;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.AssertUtil;
import cn.wolfcode.p2p.base.util.BitStateUtil;
import cn.wolfcode.p2p.base.util.UserContext;

@Service
@Transactional
public class VideoAuthServiceImpl implements IVideoAuthService {

	@Autowired
	private OrderTimeMapper orderTimeMapper;

	@Autowired
	private VideoAuthMapper authMapper;

	@Autowired
	private IUserInfoService userinfoService;

	@Override
	public VideoAuth get(Long videoAuthId) {
		return this.authMapper.selectByPrimaryKey(videoAuthId);
	}

	@Override
	public PageInfo<VideoAuth> query(VideoAuthQueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<VideoAuth> list = this.authMapper.queryData(qo);
		return new PageInfo<>(list);
	}

	@Override
	public void order(VideoAuth to) {
		// 检查,没有视频认证,且没有审核中的
		UserInfo current = this.userinfoService.get(UserContext.getCurrent().getId());
		AssertUtil.isTrue(!current.getHasVideoAuth(), "用户已经视频认证");
		AssertUtil.isTrue(current.getVideoAuthId() == null, "用于已经提交视频申请");
		// 创建一个视频认证对象,设置相关属性;
		VideoAuth va = new VideoAuth();
		va.setApplier(UserContext.getCurrent());
		va.setApplyTime(new Date());
		va.setAuditor(to.getAuditor());
		va.setOrderDate(to.getOrderDate());
		va.setOrderTime(to.getOrderTime());
		va.setState(VideoAuth.STATE_NORMAL);
		// 保存
		this.authMapper.insert(va);
		// 把id设置给userinfo;
		current.setVideoAuthId(va.getId());
		this.userinfoService.update(current);
	}

	@Override
	public List<OrderTime> listOrderTimes() {
		return this.orderTimeMapper.selectAll();
	}

	@Override
	public void audit(Long id, int state, String remark) {
		VideoAuth va = this.authMapper.selectByPrimaryKey(id);
		// 判断,申请用户没有通过视频认证,视频审核对象状态为待审核状态
		if (va != null && va.getState() == VideoAuth.STATE_NORMAL) {
			UserInfo applier = this.userinfoService.get(va.getApplier().getId());
			if (!applier.getHasVideoAuth()) {
				// 设置相关审核属性
				va.setAuditTime(new Date());
				va.setRemark(remark);
				va.setState(state);
				if (state == VideoAuth.STATE_PASS) {
					// 如果审核通过,添加状态吗
					applier.addState(BitStateUtil.OP_VEDIO_AUTH);
				} else {
					// 如果审核拒绝,userinfo的videoauthid删除;
					applier.setVideoAuthId(null);
				}
			}
			this.authMapper.updateByPrimaryKey(va);
			this.userinfoService.update(applier);
		}
	}

}
